﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTEKGB : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTEKGBData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdndate = "", enddndate = "";
        private string fromdate = "", enddate = "";
        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtFLFD.Text = value;
            }
        }
        #endregion
        public JLTEKGB()
        {
            InitializeComponent();
            this.Name = "采购入库信息";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new JLTEKGBData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("FROMDNDATE", typeof(string));
            tableUI.Columns.Add("ENDDNDATE", typeof(string));
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
            tableUI.Columns.Add("FKRS", typeof(string));
            tableUI.Columns.Add("FLFD", typeof(string));
            tableUI.Columns.Add("SUPP", typeof(string));
            tableUI.Columns.Add("ARTICLE", typeof(string));
            dwUI = tableUI.NewRow(); 
        }
        public override void OnWorkflowIDChange(string wfid)
        {
            base.OnWorkflowIDChange(wfid); 
            try
            {
                dwWFEvent = null;
                DataSet SResult = m_Data.INTEXDBC.QueryICC_WFEVENTS(wfid, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwWFEvent = row;
                }
                if (dwWFEvent == null)
                    return;
                string events = dwWFEvent["WFEVENTS"].ToString().Trim();
                switch (events)
                {
                    case "A901102":
                        KRS = dwWFEvent["KRS"].ToString();
                        LFD = dwWFEvent["LFD"].ToString();
                        txtSUPP.Text = dwWFEvent["HOW"].ToString();
                        btnQuery_Click(null, null);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                
                txtFKRS.Focus();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    string FKRS = txtFKRS.Text.Trim();
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    string SUPP = m_Data.Right("00000000" + txtSUPP.Text.Trim(), 8);
                    string ARTICLE = txtARTICLE.Text.Trim();

                    if (FKRS.Trim() == "")
                    {
                        FKRS = "AA";
                    }
                    if (fromdndate == "")
                    {
                        fromdndate = "0";
                    }
                    if (enddndate == "")
                    {
                        enddndate = "99999999";
                    } 
                    if (fromdate == "")
                    {
                        fromdate = "20000000";
                    }
                    if (enddate == "")
                    {
                        enddate = "99999999";
                    }
                    dwUI["FROMDNDATE"] = fromdndate;
                    dwUI["ENDDNDATE"] = enddndate; 
                    dwUI["FROMDATE"] = fromdate;
                    dwUI["ENDDATE"] = enddate;
                    dwUI["FKRS"] = FKRS;
                    dwUI["FLFD"] = LFD;
                    dwUI["SUPP"] = SUPP;
                    dwUI["ARTICLE"] = ARTICLE;
                    setResult = m_Data.BuildJLTEKGBData(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["WASTAMM"];
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
                
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new JLTEKGBData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            JLTEKGBReport m_rpt = new JLTEKGBReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }

                
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

        private void FROM_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdndate = "";
                fromdndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddndate = "";
                enddndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }


    }
}
